package com.lovo.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lovo.config.ApplicationConfig;
import com.lovo.model.StudentDO;
import com.lovo.service.IStudentService;
import com.lovo.service.StudentService;
import com.lovo.vo.ResultVO;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "UpdateStuServlet",value = "/update")
public class UpdateStuServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id=Integer.parseInt(request.getParameter("id"));
        String name =request.getParameter("name");
        int pwd =Integer.parseInt(request.getParameter("age"));
        StudentDO studentDO=new StudentDO(name,pwd);
        studentDO.setId(id);
        IStudentService studentService;
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);
        studentService = context.getBean(StudentService.class);
        studentService.update(studentDO);
        ResultVO resultVO = new ResultVO();
        resultVO.setStatus(1);
        response.getWriter().print(new ObjectMapper().writeValueAsString(resultVO));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
